﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using IIngredient;
using System.Configuration;
using Ingredient.App_Data;
using Ingredient.App_Data.ingredientsDataSetTableAdapters;

namespace Ingredient
{
    //使用ACCESS数据库
    public class AccessIngredientData:IIngredientData
    {
        /// <summary>
        /// 获取所有食材的标准名称
        /// </summary>
        /// <returns>所有食材的标准名称</returns>
        public List<string> GetAllIngredientsNames()
        {
            return GetData("select DefaultName from Ingredients");
        }

        /// <summary>
        /// 获取与输入名称最接近的食材的标准名称
        /// </summary>
        /// <param name="originalName">输入名称</param>
        /// <returns>与输入名称最接近的食材的标准名称</returns>
        public List<string> GetSimilarIngredientsNames(string originalName)
        {
            return GetData(String.Format("select DefaultName from Ingredients where [Alias] like '{0}' OR [DefaultName] = '{0}'",originalName));
        }

        //从配置文件中获取连接字符串
        //private string GetConnectionString()
        //{
        //    return ConfigurationManager.ConnectionStrings[0].ConnectionString;
        //}

        //在数据库中查询
        private static List<string> GetData(string sqlCommand)
        {
            DataSet ds = new DataSet();
            DataAdapter da = new OleDbDataAdapter(sqlCommand, "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|/App_Data/ingredients.accdb;");
            da.Fill(ds);
            return (from DataRow dr in ds.Tables[0].Rows select dr[0].ToString()).ToList();
        }
    }
}
